#include "../cse452.h"
#include "MyScene.h"

void MyScene::resize(int w, int h) {
    // resize the film plane to the specified width/height
    camera.setWidthHeight(w, h);
}

/// Note: your camera and screen clear, etc, will be set up by
/// SceneviewUI.cpp *before* this gets called
void MyScene::draw() {
    // render the scene using OpenGL
    if (!isLoaded) // Don't draw if loadSceneFile hasn't been called yet
        return;


    // Turn off all lights
    for ( int i = 0; i < 7; i++ )
        glDisable( GL_LIGHT0 + i );

    //  .. and reset
    glLightModelfv( GL_LIGHT_MODEL_AMBIENT, &ambientLight[0] );
    for (unsigned int i = 0; i < lights.size(); i++) {
        lights[i].setOpenGLLight( GL_LIGHT0 + i );
    }

    // TODO: draw the rest of the scene here
	
	////////////////////////////////////////////////////////////////////////////
	//TODO Iterate through objectList and alphaObjectList and draw the values///
	////////////////////////////////////////////////////////////////////////////
	if(!objectList.empty()){
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        for(std::list<MatrixObject>::iterator it = objectList.begin();
			it != objectList.end();
			it++){
#if _USELISTS
			while(glGetError() != GL_NO_ERROR)
				std::cout << "MyScene.draw found GLError" << std::endl;
			if(it->listName==0){
				it->listName = glGenLists(GLsizei(1));
				glNewList(it->listName, GL_COMPILE_AND_EXECUTE);
				glPushMatrix();
				glMultMatrixd(&((it->m)(0,0)));
				it->o->draw();
				glPopMatrix();
				glEndList();

				//Failed due to not initialized workspace resetting to try again later
				if(glGetError() != GL_NO_ERROR){
					std::cout << "MyScene.draw Error when drawing list " << it->listName << std::endl;
					glDeleteLists(it->listName, 1);
					it->listName = 0;
				}
			}
			else {
				std::cout << "Drawing list " << it->listName << std::endl;
				glCallList(it->listName);
#if _DEBUG
				while(glGetError() != GL_NO_ERROR){
					std::cout << "GLError caused by drawing list " << it->listName << std::endl;
				}
#endif
			}
#else
			glPushMatrix();
			glMultMatrixd(&((it->m)(0,0)));
			it->o->draw();
			glPopMatrix();
#endif
		}
		glDisable(GL_BLEND);
	}
}
